// 学生信息
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <!-- <el-col :span="4" :xs="24">
        <DeptOptions @accessSchool="accessSchool" :type="2" />
      </el-col> -->

      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="学 校" prop="schoolId">
            <el-select
              v-model="queryParams.schoolId"
              placeholder="选择全部"
              clearable
              style="width: 240px"
              @change="schoolChange"
            >
              <el-option
                v-for="item in schoolLiat"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="班 级" prop="className">
            <el-input
              v-model="queryParams.className"
              placeholder="请输入班级"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
            <!-- <el-select
              v-model="queryParams.classId"
              placeholder="选择全部"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in classlLiat"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>-->
          </el-form-item>
          <el-form-item label="民 族" prop="nation">
            <el-select
              v-model="queryParams.nation"
              placeholder="选择全部"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in nationLiat"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="性 别" prop="sex">
            <el-select
              v-model="queryParams.sex"
              placeholder="选择全部"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="姓 名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入学生姓名"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="标 签" prop="flag">
            <el-select
              v-model="queryParams.flag"
              placeholder="选择全部"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in labelLiat"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状 态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item> -->
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:user:add']"
              >新增</el-button
            >
          </el-col>

          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['system:user:import']"
              >导入</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:user:export']"
              >导出</el-button
            >
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="userList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="姓名"
            align="center"
            key="name"
            prop="name"
            width="100"
          >
            <template slot-scope="scope">
              <div class="labelys">
                <div></div>
                <div>{{ scope.row.name }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="联系方式"
            align="center"
            key="phoneNum"
            prop="phoneNum"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="性别"
            align="center"
            key="sex"
            prop="sex"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <div>
                {{ selectDictLabels(dict.type.sys_user_sex, scope.row.sex) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="民族"
            align="center"
            key="nation"
            prop="nation"
          />
          <el-table-column
            label="年龄"
            align="center"
            key="age"
            prop="age"
            width="120"
          />
          <el-table-column
            label="所属学校"
            align="center"
            key="schoolName"
            prop="schoolName"
            width="120"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="班级"
            align="center"
            key="className"
            prop="className"
            width="120"
          />
          <el-table-column label="标签" align="center" key="flag">
            <template slot-scope="scope">
              <div>
                {{ selectDictLabels(dict.type.sys_label_type, scope.row.flag) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit-outline"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:user:edit']"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-share"
                @click="handleUpdate(scope.row, '详情')"
                v-hasPermi="['system:user:edit']"
                >详情</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:user:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <div class="tater">
        <div
          :class="tateType == '1' ? 'choice' : 'uncheck'"
          @click="tateType = '1'"
        >
          基本信息
        </div>
        <div
          v-if="title !== '新增学生'"
          :class="tateType == '2' ? 'choice' : 'uncheck'"
          @click="tateType = '2'"
        >
          教育经历
        </div>
        <div
          v-if="title !== '新增学生'"
          :class="tateType == '3' ? 'choice' : 'uncheck'"
          @click="tateType = '3'"
        >
          家庭情况
        </div>
      </div>
      <div v-if="tateType == '1'">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="学生姓名" prop="name">
                <el-input
                  v-model="form.name"
                  placeholder="请输入学生姓名"
                  :disabled="displays"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="学 校" prop="schoolId">
                <el-select
                  v-model="form.schoolId"
                  placeholder="请选择所属学校"
                  @change="schoolChange"
                  :disabled="displays"
                >
                  <el-option
                    v-for="item in schoolLiat"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="phoneNum">
                <el-input
                  v-model="form.phoneNum"
                  placeholder="联系方式"
                  maxlength="11"
                  :disabled="displays"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="班 级" prop="classId">
                <el-select
                  v-model="form.classId"
                  placeholder="请选择所属年级"
                  :disabled="displays"
                >
                  <el-option
                    v-for="item in classlLiat"
                    :key="item.key"
                    :label="item.value"
                    :value="item.key"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="学籍号"
                prop="studentCode"
                :disabled="displays"
              >
                <el-input
                  v-model="form.studentCode"
                  :disabled="displays"
                  placeholder="请输入学生学籍号"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="年 龄">
                <el-input
                  v-model="form.age"
                  placeholder="请输入学生年龄"
                  :disabled="displays"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性 别" prop="sex">
                <el-select
                  v-model="form.sex"
                  placeholder="请选择性别"
                  :disabled="displays"
                >
                  <el-option
                    v-for="item in dict.type.sys_user_sex"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="民 族">
                <el-select
                  v-model="form.nation"
                  placeholder="选择民族"
                  clearable
                  :disabled="displays"
                >
                  <el-option
                    v-for="dict in nationLiat"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status" :disabled="displays">
                  <el-radio
                    v-for="dict in dict.type.sys_normal_disable"
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input
                  v-model="form.remark"
                  type="textarea"
                  placeholder="请输入内容"
                  :disabled="displays"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!--教育经历 -->
      <div v-if="tateType == '2'" style="width: 100%">
        <el-table :data="experience">
          <el-table-column
            label="起止时间"
            align="center"
            key="name"
            prop="name"
          />
          <el-table-column
            label="所属学校"
            align="center"
            key="schoolName"
            prop="schoolName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="状 态"
            align="center"
            key="className"
            prop="className"
            width="120"
          />
        </el-table>
      </div>
      <!-- 家庭情况 -->
      <div v-if="tateType == '3'">
        <el-table :data="family">
          <el-table-column
            label="关 系"
            align="center"
            key="name"
            prop="name"
          />
          <el-table-column
            label="姓 名"
            align="center"
            key="schoolName"
            prop="schoolName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="学 历"
            align="center"
            key="className"
            prop="className"
            width="120"
          />
          <el-table-column
            label="职 业"
            align="center"
            key="className"
            prop="className"
            width="120"
          />
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!displays"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" />
            是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DeptOptions from "@/components/DeptOptions";

import {
  schoolPage, //获取学校
  personStudent, //新增学生
  personStudentPut, //修改学生信息
  studentPage, //获取学生信息
  studentNations, //获取民族
  delDent, //删除学生
  classPull, //获取班级
} from "@/api/information/index";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "User",
  dicts: ["sys_normal_disable", "sys_user_sex", "sys_label_type"],
  components: { Treeselect, DeptOptions },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 教育经历
      experience: [],
      // 家庭情况
      family: [],
      // 是否显示弹出层
      open: false,
      displays: false,
      // 表单参数
      form: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 部门名称
      deptName: undefined,
      // 学校信息
      schoolLiat: [],
      //   班级
      classlLiat: [],
      // 名族
      nationLiat: [],
      visibleSync: false,
      pupilProfile: {},
      tateType: "1",
      //   标签
      labelLiat: [
        { value: 1, label: `重度` },
        { value: 2, label: `中度` },
        { value: 3, label: `轻度` },
        { value: 4, label: `正常` },
      ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" },
          {
            // min: 2,
            // max: 20,
            // message: "用户名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        schoolId: [
          { required: true, message: "所属学校不能为空", trigger: "blur" },
        ],
        studentCode: [
          { required: true, message: "所属学籍号不能为空", trigger: "blur" },
        ],
        classId: [
          {
            required: true,
            message: "请先选择有班级的学校,班级不能为空",
            trigger: "blur",
          },
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        phoneNum: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getschool();
    this.getList();
    this.getNations();
  },
  methods: {
    // 获取民族
    getNations() {
      studentNations().then((res) => {
        if (res.code == 200) {
          this.nationLiat = res.data;
        }
      });
    },
    // 获取学校
    getschool() {
      schoolPage({ type: 2 }).then((res) => {
        this.schoolLiat = res.rows.map((item) => {
          return {
            value: item.id,
            label: item.name,
          };
        });
      });
    },
    //
    schoolChange(val) {
      this.classlLiat = [];
      console.log("数据", val);
      if (val) {
        classPull(val).then((res) => {
          this.classlLiat = res.data;
          if (this.classlLiat.length == 0) {
            this.form.classId = "";
          }
        });
      }
    },

    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length !== 1;
    },
    /** 查询学生信息列表 */
    getList() {
      this.loading = true;
      studentPage(this.queryParams).then((res) => {
        console.log("获取", res);
        this.userList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    accessSchool(data) {
      this.queryParams.pageNum = 1;
      this.queryParams.schoolId = data.id.toString();
      this.getList();
      console.log("学校信息", data);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,

        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      // this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.names = selection.map((item) => item.name);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleResetPwd":
          this.handleResetPwd(row);
          break;
        case "handleTutorship":
          this.handleTutorship(row);
          break;
        case "handleAuthRole":
          this.handleAuthRole(row);
          break;
        default:
          break;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "新增学生";
      this.reset();
    },
    /** 编辑按钮操作 */
    handleUpdate(row, type) {
      if (type == "详情") {
        this.title = "学生详情";
        this.displays = true;
      } else {
        this.title = "修改学生";
        this.displays = false;
      }
      this.open = true;
      this.form = row;
      this.form.schoolId = row.schoolId;
      this.schoolChange(this.form.schoolId);
    },

    /** 提交按钮 */
    submitForm: function () {
      this.schoolLiat.forEach((element) => {
        if (this.form.schoolId == element.value) {
          this.form.schoolName = element.label;
        }
      });
      this.classlLiat.forEach((element) => {
        if (this.form.classId == element.key) {
          this.form.className = element.value;
        }
      });
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            personStudentPut(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            personStudent(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids;
      const namec = row.name || this.names;
      this.$modal
        .confirm('是否确认删除用户编号为"' + namec + '"的数据项？')
        .then(function () {
          return delDent(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/user/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "system/user/importTemplate",
        {},
        `user_template_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style lang="scss" scoped>
.labelys {
  width: 100%;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  div:nth-child(1) {
    width: 10px;
    height: 10px;
    background-color: yellowgreen;
    margin-right: 10px;
  }
}
.tater {
  display: flex;
  margin-top: -30px;
  margin-bottom: 10px;
  .choice {
    width: 80px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    margin: 0 10px;
    color: #1890ff;
    border-bottom: 2px solid #1890ff;
  }
  .uncheck {
    width: 80px;
    height: 25px;
    line-height: 25px;
    text-align: center;
    margin: 0 10px;
  }
  .uncheck:hover {
    color: #1890ff;
  }
}
</style>
